package com.lcq.study.common;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


public class PyTest {
    public static void main(String[] args) throws IOException {
        String pyFileName = "D:/test/python/beijing-test.py";
        execPy(pyFileName);
    }

    private static void execPy(String pyFile) throws IOException {
        String cmds = String.format("python %s", pyFile);
        BufferedReader in = null;
        try {
            Process pcs = Runtime.getRuntime().exec(cmds);
            pcs.waitFor();
            in = new BufferedReader(new InputStreamReader(pcs.getInputStream(),"GB2312"));
            boolean success = false;
            String result;
            while ((result = in.readLine()) != null){
                System.out.println(result);
                if (result.equals("success!!!!")){
                    success = true;
                    break;
                }
            }

            if (!success){
                throw new RuntimeException("执行py程序出错！");
            }

            System.out.println("执行成功");
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            if (in != null){
                in.close();
            }
        }
    }
}
